<template>
  <main class="content">
    <div style="padding-top: 20vh;">
      <h1 style="font-size: 22px; text-align: center;">登录</h1>
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field v-model="username" name="用户名" label="用户名" placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]" />
          <van-field v-model="password" type="password" name="密码" label="密码" placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]" />
        </van-cell-group>
        <div style="margin: 16px;">
          <van-button round block type="primary" native-type="submit">
            提交
          </van-button>
        </div>
      </van-form>
    </div>
  </main>
</template>
<script setup>
import { showNotify } from 'vant'
import { login } from "@/api";
import router from "@/router";

const username = ref('111');
const password = ref('111');
const onSubmit = (values) => {
  login({ name: username.value, password: password.value }).then((res) => {
    if (res.data !== "") {
      localStorage.setItem('token', res.data);
      router.push({ path: '/main' });
    } else {
      showNotify({ type: 'danger', message: "登录失败" })
    }
  })
};

onMounted(() => {
  localStorage.removeItem("token")
})
</script>
<style lang="scss" scoped>
.content {
  height: 100vh;
  background: #f9fafc;
}
</style>
